<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/6
 * Time: 上午9:49
 */
class GoodsService extends Service{

    public static function indexHeader(){
        return array(
        		'id'         => array('text' => 'ID', 'rule' => 'int'),
        		'g_id'       => array('text' => '物品标识'),
        		'name'       => array('text' => '物品名称'), 
        		'quality'    => array('text' => '物品品质'), 
        		'stage'      => array('text' => '物品阶数'),
        		'star'       => array('text' => '物品星级'),
        		'access'     => array('text' => '获取途径'),
        		'max_overlap'     => array('text' => '最大堆叠数'),
        		'item_value' => array('text' => '物品价值(元宝)'),
        		'description'=> array('text' => '物品描述'),
                'action'     => array('text' => '操作')
        );
    }
    
    public static function indexData($params){
    	$modGoods = new ModGood();
    	$result = $modGoods->page($params); 
    	$dictTmp = PubDict::dict($params['g'], 'colortype');
    	foreach($result['data'] as &$v){ 
    		$color = self::mapColor($v['quality']);
    		$v['quality'] = $dictTmp[$v['quality']];
    		
    		$v['name'] = '<font color="'.$color.'">'.$v['name'].'</font>';
    		
    		$description = preg_replace('/(\[color=#)([a-zA-Z0-9]){6}(])/', '', $v['description']);
    		$description = preg_replace('/(\[\/color\])/', '', $description);
    		$description = str_replace("\\n","",$description);    		
    		// (\[color=#)([a-zA-Z0-9]){6}(])(.*)(\[\/color\])
    		//$str = preg_replace('/\[color=(.*)\](.*)\[\/color\]/', '<font color=$1>$2</font>', $str);
    		$v['description'] = $description;
    		//$v['action']    = ' <a href="'.PubFunc::buildUrl('/oss/?r=goods/edit').'&id='.$v['id'].'">编辑</a>';
    		$v['action']   .= ' <a href="'.PubFunc::buildUrl('/oss/?r=goods/delete').'&id='.$v['id'].'" confirm="是否要删除该条信息?">删除</a>';
    	}
    	unset($v);
    	return $result;
    }
    
    
    
    
    public static function delete($id){
    	if(empty($id)) return false;
    
    	$modGoods = new ModGood();
    	$modGoods->deleteByPk($id);
    	return true;
    }
    
    public static function upload($files){
    	$file = new PubFile();
    	$fileName = $files["file"]["name"];
    	$suffixArr = array('xls','xlsx','xlsm'); //允许上传的文件后缀
    	$check= $file::checkSuffix($fileName,$suffixArr);
    	if($check){
    		$ext = $file::getSuffix($fileName);
    		$uploadDir = '../public/upload/goods/'.date('Ym').'/';
    		$uploadName = md5(microtime(true)).'.'.$ext;
    		$upload = $file::uploadFile($files["file"],$uploadDir,$uploadName);
    		if(!$upload){
    			return array('status' => 0, 'msg' =>$upload,'data'=>'');
    		}else{
    			$path = dirname(dirname(dirname(dirname(__File__)))).'/public/upload/goods/'.date('Ym').'/'.$uploadName;  //获取绝对路径
    			return array('status' => 1, 'msg' =>'上传成功','data'=>$path);
    		}
    	}else{
    		return array('status' => 0, 'msg' =>'上传文件后缀不允许!','data'=>'');
    	}
    }
    
    public static function xlsAdd($data){
    	$modGoods = new ModGood();
    	unset($data[1]);
    	unset($data[2]);
    	unset($data[3]);
    	unset($data[4]);
    	
    	$add_data = array();
    	$gid_array = $modGoods->getGid();
    	
    	$gid_in = array();
    	foreach ($gid_array as $key=>$val){
    		$gid_in[] = $val['g_id'];
    	}
    	$game = PubBase::getCurrentGame();
        $all_num = count($data);
        $success_num = 0;
        $error_num = 0;
    	foreach($data as $v){
    		if(in_array($v['A'], $gid_in)){  //已经存在
    			$error_num++;
    			continue;
    		}
    		$d = array();
    		$d['game'] = $game;
    		$d['g_id'] = $v['A'];
    		$d['name'] = $v['B'];
    		$d['note'] = $v['C'];
    		$d['type'] = $v['D'];
    		$d['filter_type'] = $v['E'];
    		$d['sub_type'] = $v['F'];
    		$d['sort'] = $v['G'];
    		$d['level'] = $v['H'];
    		$d['use_level'] = $v['I'];
    		$d['quality'] = $v['J'];
    		$d['stage'] = $v['K'];
    		$d['star'] = $v['L'];
    		$d['gem_hole_count'] = $v['M'];
    		$d['gem_type'] = $v['N'];
    		$d['sui_xian'] = $v['O'];
    		$d['sui_god'] = $v['P'];
    		$d['occupation'] = $v['Q'];
    		$d['rank'] = $v['R'];
    		$d['use_count'] = $v['S'];
    		$d['locked'] = $v['T'];
    		$d['expired'] = $v['U'];
    		$d['max_overlap'] = $v['V'];
    		$d['attribute_id'] = $v['W'];
    		$d['intensify_id'] = $v['X'];
    		$d['resource_id'] = $v['Y'];
    		$d['sell_price'] = $v['Z'];
    		$d['one_key_sale'] = $v['AA'];
    		$d['description'] = $v['AB'];
    		$d['access'] = $v['AC'];
    		$d['use_effect'] = $v['AD'];
    		$d['can_use'] = $v['AE'];
    		$d['auto_use'] = $v['AF'];
    		$d['devour_exp'] = $v['AG'];
    		$d['depot_point'] = $v['AH'];
    		$d['model_id'] = $v['AI'];
    		$d['effects'] = $v['AJ'];
    		$d['recommend_attributes'] = $v['AK'];
    		$d['is_lot_use'] = $v['AL'];
    		$d['is_fast_tip'] = $v['AM'];
    		$d['gift_id'] = $v['AN'];
    		$d['drop_limit_type'] = $v['AO'];
    		$d['drop_limit_day'] = $v['AP'];
    		$d['drop_protect_time'] = $v['AQ'];
    		$d['drop_survive_time'] = $v['AR'];
    		$d['get_effect'] = $v['AS'];
    		$d['item_value'] = $v['AT'];
    		$d['background_show'] = $v['AU'];
    		$d['is_analysis'] = $v['AV'];
    		array_push($add_data, $d);
    	}
     	
    	$success_num = $all_num - $error_num;
    	$msg = '导入成功 '.$success_num.' 条，失败 '.$error_num.' 条';
    	$result = $modGoods->addmuch($add_data);
    	return array('status' => 1, 'msg' =>$msg,'data'=>'');
    }
    
    public static function autoComplete($val,$type){
    	$modGoods = new ModGood();
    	$params = array();
    	$game = PubBase::getCurrentGame();
    	$params['g'] = $game;
    	if($type == 1){  //查询物品名称
    		$params['name'] = $val;
    	}else if($type == 2){  //查询物品标识
    		$params['g_id'] = $val;
    	}

    	$result = $modGoods->select($params);
    	$complte = array();
    	
    	if($type == 1){
    		foreach ($result as $k=>$v){
    			$complte[$k] = $v['name'];
    		}
    	}
    	
    	if($type == 2){
    		foreach ($result as $k=>$v){
    			$complte[$k] = $v['g_id'];
    		}
    	}
    	

    	$complte_json = json_encode($complte);
    	return $complte;
    	
    }
    
    
    public static function getGid($val){
    	$modGoods = new ModGood();
    	$obj = $modGoods->find('name=:name', array(':name' => $val));   	
    	$gid = '';
    	$max_overlap = '';
    	if($obj){
    		$info = $obj->getAttributes();
    		$gid = $info['g_id'];
    		$max_overlap = $info['max_overlap'];
    		$description = $info['description'];
    	}
    	
    	$description = preg_replace('/(\[color=#)([a-zA-Z0-9]){6}(])/', '', $description);
    	$description = preg_replace('/(\[\/color\])/', '', $description);
    	$description = str_replace("\\n","",$description);
    	$result = array('gid'=>$gid,'max_overlap'=>$max_overlap,'description'=>$description);
    	return $result;   	
    } 
    
    
    public static function getName($val){
    	$modGoods = new ModGood();
    	$obj = $modGoods->find('g_id=:g_id', array(':g_id' => $val));
    	$gid = '';
    	$max_overlap = '';
    	if($obj){
    		$info = $obj->getAttributes();
    		$name = $info['name'];
    		$max_overlap = $info['max_overlap'];
    		$description = $info['description'];
    	}
    	$description = preg_replace('/(\[color=#)([a-zA-Z0-9]){6}(])/', '', $description);
    	$description = preg_replace('/(\[\/color\])/', '', $description);
    	$description = str_replace("\\n","",$description);
    	$result = array('name'=>$name,'max_overlap'=>$max_overlap,'description'=>$description);
    	return $result; 
    	 
    }

    
}